package guestbook;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import com.google.appengine.api.rdbms.AppEngineDriver;
import javax.servlet.http.*;
import java.sql.*; 
 

@SuppressWarnings("serial")
public class GuestbookrdbServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
/*		
		Connection c = null; 	
		PrintWriter out = resp.getWriter(); 
		try
		{
		 DriverManager.registerDriver(new AppEngineDriver());       
		 c = DriverManager.getConnection("jdbc:google:rdbms://hwangtest317:mygql/guestbook");       
		 String fname = req.getParameter("fname");     
		 if ( fname.isEmpty()  || fname.equals("") )
			 fname ="hkw";		
		 String content = req.getParameter("content");
		 if ( content.isEmpty()  || content.equals("") )
			 content ="1111111111114";
		  
//		 if (fname == "" || content == "") {         
//			 out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");       
//		 } else {       
				 String statement ="INSERT INTO entries (guestName, content) VALUES( ? , ? )";       
				 PreparedStatement stmt = c.prepareStatement(statement);       
				 stmt.setString(1, fname);       
				 stmt.setString(2, content);       
				 int success = 2;       
				 success = stmt.executeUpdate();       
				 if(success == 1) {         
					 out.println("<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");       
				 } else if (success == 0) {         
					 out.println("<html><head></head><body>Failure! Please try again! Redirecting in 3 seconds...</body></html>");       
//			 } 
		 }
	     } 
		catch (SQLException e) 
		{         e.printStackTrace();       } 
		finally {           
			if (c != null)             
				try {               c.close();             
				} catch (SQLException ignore) {               }       
				}
				*/ 	 
		}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)   throws IOException { 
		Connection c = null; 	
		PrintWriter out = resp.getWriter(); 
		try
		{
		 DriverManager.registerDriver(new AppEngineDriver());       
		 c = DriverManager.getConnection("jdbc:google:rdbms://hwangtest317:mygql/guestbook");       
		 String fname = req.getParameter("fname");     
		 if ( fname.isEmpty()  || fname.equals("") )
			 fname ="hkw";		
		 String content = req.getParameter("content");
		 if ( content.isEmpty()  || content.equals("") )
			 content ="1111111111114";
		  
		 if (fname == "" || content == "") {         
			 out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");       
		 } else {       
				 String statement ="INSERT INTO entries (guestName, content) VALUES( ? , ? )";       
				 PreparedStatement stmt = c.prepareStatement(statement);       
				 stmt.setString(1, fname);       
				 stmt.setString(2, content);       
				 int success = 2;       
				 success = stmt.executeUpdate();       
				 if(success == 1) {         
					 out.println("<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");       
				 } else if (success == 0) {         
					 out.println("<html><head></head><body>Failure! Please try again! Redirecting in 3 seconds...</body></html>");       
			 } 
		 }
	     } 
		catch (SQLException e) 
		{         e.printStackTrace();       } 
		finally {           
			if (c != null)             
				try {               c.close();             
				} catch (SQLException ignore) {               }       
				} 	 
	
	}
}
